<template>
  <div
    :id="`LArk-container${mapKey ? '-' + mapKey : ''}`"
    :class="[
      'LArk-container',
      customClass,
      { 'LArk-container-compare-rh': compare },
    ]"
  ></div>
</template>

<script>
import Vue from "vue";
import axios from "axios";
export default {
  name: "CesiumViewer",
  data() {
    return {
      basemaps: [],
    };
  },
  props: {
    // 初始化配置参数
    url: String,
    widgetUrl: String,

    // 地图唯一性标识
    mapKey: {
      type: String,
      default: "",
    },

    // 自定义参数
    options: Object,

    // 是否分屏显示
    compare: {
      type: Boolean,
      default: false,
    },

    // 是否插入到body元素上
    appendToBody: {
      type: Boolean,
      default: false,
    },

    // 自定义css类名
    customClass: {
      type: String,
      default: "",
    },
  },
  created() {},
  mounted() {
    this.$nextTick(() => {
      var that = this;
      if (that.appendToBody) {
        document.body.appendChild(that.$el);
      }
      if (that.mapKey) {
        that.initMars3d(that.options);
      } else {
        that.getMapConfig(that.url).then((data) => {
          that.initMars3d(data);
        });
      }
    });
  },

  beforeDestory() {
    this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
    this.handler.destroy();
    delete this.handler;

    this[`viewer${this.mapKey}`].LArkgis.destroy();
    this[`viewer${this.mapKey}`].destroy();
    delete this[`viewer${this.mapKey}`];
  },

  methods: {
    getMapConfig(url) {
      return new Promise((resolve, reject) => {
        axios
          .get(url)
          .then((res) => {
            resolve(res.data);
          })
          .then((error) => {
            reject(error);
          });
      });
    },

    initMars3d(options) {
      var _this = this;
      const viewer = LArk.createMap({
        id: `LArk-container${this.mapKey ? `-${this.mapKey}` : ""}`,
        data: options.map3d,
        serverURL: options.serverURL,
        ...this.options,
      });
      //widget处理
      this.getMapConfig(this.widgetUrl).then((data) => {
        this.initStaticWidget(viewer, data);
      });

      viewer.LArkgis.openFlyAnimation(function () {
        
      });

      viewer.LArkgis.click = () => {
        // 触发 document 上的事件
        document.dispatchEvent(new Event("mousedown"));
        document.dispatchEvent(new Event("click"));
      };
      if(this.mapKey) {
        this[`viewer${this.mapKey}`] = viewer;
        Vue.prototype[`$viewer${this.mapKey}`] = viewer;
      }else {
        Vue.prototype.$viewer = viewer;
      }
      // 挂载到全局对象下，所有组件通过this.$viewer访问
      // Vue.prototype[`$viewer${this.mapKey}`] = viewer;
      Vue.prototype.$Cesium = Cesium;
      console.log(">>>>> 地图创建成功 >>>>");
      this.$emit("onload", viewer);

      window.Cesium = Cesium;
      window.viewer = viewer;
    },

    //初始化外部静态widget功能（兼容使用传统模式开发的一些widget）
    initStaticWidget(viewer, widget) {
      LArk.widget.init(viewer, widget);
      //绑定图层管理
      window.bindToLayerControl = function (options) {
        var layer = new LArk.layer.BaseLayer(options, viewer);
        var manageLayersWidget = LArk.widget.getClass(
          "widgets/manageLayers/widget.js"
        );
        if (manageLayersWidget) {
          manageLayersWidget.addOverlay(options);
        }
        return layer;
      };
      //取消绑定图层管理 ， 参数为bindToLayerControl返回的图层
      window.unbindLayerControl = function (layer) {
        viewer.LArkgis.removeOperationalLayer(layer.config.id);

        var manageLayersWidget = LArk.widget.getClass(
          "widgets/manageLayers/widget.js"
        );
        if (manageLayersWidget) {
          manageLayersWidget.removeLayer(layer.config.name);
        }
      };
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss">
.LArk-container {
  height: 100%;
  overflow: hidden;
  position: relative;
  left: 0;
  right: 0;
}

/* 重写Cesium的css */

/**cesium按钮背景色*/
.cesium-button {
  background-color: #ffffff;
  color: #7bedfb;
  fill: #7bedfb;
  // box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
  line-height: 32px;
}

.cesium-viewer-geocoderContainer .cesium-geocoder-input {
  background-color: rgba(63, 72, 84, 0.7);
}

.cesium-viewer-geocoderContainer .cesium-geocoder-input:focus {
  background-color: rgba(63, 72, 84, 0.9);
}

.cesium-viewer-geocoderContainer .search-results {
  background-color: #3f4854;
}

.cesium-geocoder-searchButton {
  background-color: #ffffff;
}

.cesium-infoBox-title {
  background-color: #3f4854;
}

.cesium-infoBox {
  background: rgba(63, 72, 84, 0.9);
}

.cesium-toolbar-button img {
  height: 100%;
}

.cesium-performanceDisplay-defaultContainer {
  top: auto;
  bottom: 35px;
  right: 50px;
}
// .cesium-performanceDisplay-ms,
// .cesium-performanceDisplay-fps {
//   color: #fff;
// }

/**cesium工具栏位置*/
.cesium-viewer-toolbar {
  top: auto;
  left: auto;
  right: 12px;
  bottom: 35px;
}

.cesium-viewer-toolbar > .cesium-toolbar-button,
.cesium-navigationHelpButton-wrapper,
.cesium-viewer-geocoderContainer {
  margin-bottom: 5px;
  float: right;
  clear: both;
  text-align: center;
}

.cesium-baseLayerPicker-dropDown {
  bottom: 0;
  right: 40px;
  max-height: 700px;
  margin-bottom: 5px;
}

.cesium-navigation-help {
  top: auto;
  bottom: 0;
  right: 40px;
  transform-origin: right bottom;
}

.cesium-sceneModePicker-wrapper {
  width: auto;
  margin: 0 3px!important;
}

.cesium-sceneModePicker-wrapper .cesium-sceneModePicker-dropDown-icon {
  float: left;
  margin: 0 3px;
}

.cesium-viewer-geocoderContainer .search-results {
  left: 0;
  right: 40px;
  width: auto;
  z-index: 9999;
}

.cesium-infoBox-title {
  background-color: #3f4854;
}

.cesium-infoBox {
  top: 50px;
  background: rgba(63, 72, 84, 0.9);
}

/**左下工具栏菜单*/
.toolbar-dropdown-menu-div {
  background: rgba(43, 44, 47, 0.8);
  border: 1px solid #2b2c2f;
  z-index: 991;
  position: absolute;
  right: 60px;
  bottom: 40px;
  display: none;
}

.toolbar-dropdown-menu {
  min-width: 110px;
  padding: 0;
}
.toolbar-dropdown-menu > li {
  padding: 0 3px;
  margin: 2px 0;
}
.toolbar-dropdown-menu > li > a {
  color: #edffff;
  display: block;
  padding: 4px 10px;
  clear: both;
  font-weight: normal;
  line-height: 1.6;
  white-space: nowrap;
  text-decoration: none;
}

.toolbar-dropdown-menu > li > a:hover,
.dropdown-menu > li > a:focus {
  color: #fff;
  background-color: #444d59;
}

.toolbar-dropdown-menu > .active > a,
.dropdown-menu > .active > a:hover,
.dropdown-menu > .active > a:focus {
  color: #fff;
  background-color: #444d59;
}

.toolbar-dropdown-menu i {
  padding-right: 5px;
}
</style>
